<?
//impostazioni modulo Newsletter
include("includes/config.inc.php");
include("includes/auth.lib.php");
include("includes/newsletter.class.php");
include("includes/component.class.php");
include("includes/upload.class.php");
$module = "NEWSLETTER";
$num_per_page = 10;
$adminmail = "info@bsvillage.com";
   list($status, $user) = auth_get_status();
   switch($status){
       case AUTH_LOGGED:
// include Editor Start
include("FCKeditor/fckeditor.php");
// include Editor Ende
        $result1 = @mysql_query("SELECT nome,stato FROM utenti WHERE id='$id_user';");
        list($username,$level) = mysql_fetch_row($result1);
	    $menusx = "<table width=100% cellpadding=2>
					<tr>
					 <td width=100% class=\"titnewsletter\">
					  <a class=\"titnewsletter\" href=\"javascript:showHide('newsletter2');\">Newsletter</a><br>
					  <div id=newsletter2 style=\"visibility:hidden; display: none;\">
					   <ul style=\"margin-left:10px; margin-top:5px; margin-bottom:3px;\">
					    <a class=menusx href=\"newsletter.php?op=allnews\">Elenco newsletter</a><br>
						<a class=menusx href=\"newsletter.php?op=addnews\">Aggiungi newsletter</a>
					   </ul>
					  </div>
					 </td>
					</tr>
					<tr>
					 <td width=100% class=\"titnewsletter\">
					  <a class=titnewsletter href=\"javascript:showHide('group');\">Gruppi</a><br>
					  <div id=group style=\"visibility:hidden; display: none;\">
					   <ul style=\"margin-left:10px; margin-top:5px; margin-bottom:3px;\">
					    <a class=menusx href=\"newsletter.php?op=allgroup\">Elenco Gruppi</a><br>
						<a class=menusx href=\"newsletter.php?op=addgroup\">Aggiungi Gruppo</a>
					  </div>
					 </td>
					</tr>
					<tr>
					 <td width=100% class=\"titnewsletter\">
					  <a class=titnewsletter href=\"javascript:showHide('user');\">Utenti</a><br>
					  <div id=user style=\"visibility:hidden; display: none;\">
					   <ul style=\"margin-left:10px; margin-top:5px; margin-bottom:3px;\">
					    <a class=menusx href=\"newsletter.php?op=alluser\">Elenco utenti</a><br>
						<a class=menusx href=\"newsletter.php?op=adduser\">Aggiungi utente</a>
					  </div>
					 </td>
					</tr>
					<tr>
					 <td width=100% class=\"titnewsletter\">
					  <a class=titnewsletter href=\"newsletter.php?op=stat\">Report</a>
					 </td>
					</tr>  
					</table>";
			$op = $_GET['op'];
			if(!isset($op) || $op == "")
			 $op = $_POST['op'];
		switch($op)  {
			case "allnews":
			            $min = $_GET['min'];
                        $page = $_GET['page'];
                     if (!isset($min))
                      $min= 0;
                     if (!isset($page))
                      $page = 1;
			  $onload = "showHide('newsletter2');";
			  $qry = "SELECT idnews,data_sp,titolo FROM newsletter ORDER BY data_sp";
			  $newPage = new paging($qry,$min,$num_per_page,"tbtit");
			  $content = $newPage->printRecord("newsletter.php?op=modnews","newsletter.php?op=delnews","content","Data","Titolo");
			  $content .= $newPage->makepage("newsletter.php?op=allnews",$page);
			break;

			case "addnews":
			  $onload = "showHide('newsletter2');";
			  $content = "<table class=\"content\" cellspacing=\"0\" cellpadding=\"2\" width=\"700\" bgcolor=#CCCCCC border=\"1px solid #ECECEC\"><form action=\"newsletter.php\" name=\"addnews\" method=\"post\" ENCTYPE=\"multipart/form-data\">
			  			<tr>
						 <td colspan=2 align=center class=tbtit>Aggiungi newsletter</td>
						</tr>
			  			<tr>
						 <td width=100 align=right>Titolo</td>
						 <td width=600><input type=text size=60 name=titolo></td>
						</tr>
			  			<tr>
						 <td width=100 align=right>Data</td>
						 <td width=600 valign=middle><input type=text size=10 name=data_sp id=data onBlur=\"check_date(this)\"><a href=\"javascript:;\" onclick=\"return showCalendar('data', '%d/%m/%Y');\"><img src=\"imgs/date.gif\" border=0 width=20 height=18 alt=\"Calendar\"></a></td>
						</tr>
			  			<tr>
						 <td width=100 align=right>Oggetto</td>
						 <td width=600><input type=text size=60 name=oggetto></td>
						</tr>
			  			<tr>
						 <td width=100 align=right valign=top>Testo</td>
						 <td width=600>";
						//start editor
						$template = '<html>
<head>

<title>Businesshop Newsletter</title>

<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">

<style type="text/css">
<!--

.plain {
	FONT-SIZE: 11px;
	LINE-HEIGHT: 130%;
	FONT-FAMILY: Arial, Helvetica, sans-serif;
	color: #666666;
}
A.plain {
	FONT-SIZE: 11px;
	COLOR: #7584A1;
	FONT-FAMILY: Arial, Helvetica, sans-serif;
	TEXT-DECORATION: none;
	font-weight: bold;
}
A.plain:hover {
	FONT-SIZE: 11px;
	COLOR: #666666;
	FONT-FAMILY: Arial, Helvetica, sans-serif;
	TEXT-DECORATION: none;
	background-color: #EBEAEA;
}
.tit01 {
	FONT-SIZE: 18px;
	COLOR: #0B4380;
	FONT-FAMILY: Arial, Helvetica, sans-serif;
}

.prezzo_pieno {
	FONT-SIZE: 11px;
	COLOR: #666666;
	FONT-FAMILY: Arial, Helvetica, sans-serif;
	text-decoration: line-through;
}

.prezzo_scont {
	FONT-SIZE: 11px;
	COLOR: #FF3300;
	FONT-FAMILY: Arial, Helvetica, sans-serif;
}

-->
</style>
</head>
<body bgcolor="#FFFFFF" leftmargin="0" topmargin="0" marginwidth="0" marginheight="0">
<p>&nbsp;</p>
<table width="700" border="0" align="center" cellpadding="0" cellspacing="1" bgcolor="#CCCCCC">
  <tr>
    <td bgcolor="#FFFFFF"><table width="700" border="0" align="center" cellpadding="0" cellspacing="0">
      <tr>
        <td><img src="http://www.bsvillage.com/images-nwsl/header-nwsl.jpg" width="700" height="100"></td>
      </tr>
      <tr>
        <td>&nbsp;</td>
      </tr>
      <tr>
        <td><table width="700" border="0" cellspacing="0" cellpadding="10">
          <tr>
            <td><p class="tit01">titolo newsletter</p>
              <p class="plain">testo newsletter</p>
              <p class="prezzo_pieno">prezzo pieno</p>
              <p class="prezzo_scont">prezzo scontato</p></td>
          </tr>
        </table>
        </td>
      </tr>
    </table></td>
  </tr>
</table>
</body>
</html>';
						$oFCKeditor = new FCKeditor('testo');
						$oFCKeditor->Value = $template;
						$content .= $oFCKeditor->Create(); 
 						$content .= "</tr>
			  			<tr>
						 <td width=100 align=right>Allegato</td>
						 <td width=600><input type=file size=60 name=allegato></td>
						</tr>
			  			<tr>
						 <td width=100 align=right>Gruppo</td>
						 <td width=600>";
						$content .= group::menuGruppi("idgruppo"); 
						$content .= "</td>
						</tr>
						<tr>
						 <td colspan=2 align=center>
						  <input type=hidden name=op value=\"savenews\">
		 <input type=image src=\"imgs/ico_save.gif\" title=Salva onClick=\"return control_news();\" width=16 height=16>&nbsp;&nbsp;<a class=content href=\"javascript:document.addnews.submit();\"  onClick=\"return control_news();\">Salva</a> - <a href=\"javascript:document.addnews.submit();\" onclick=\"document.addnews.op.value='save_send'; onClick=\"return control_news();\"\"><img src=\"imgs/ico_mail.gif\" border=0 alt=\"Salva e invia\"></a>&nbsp;&nbsp;<a href=\"javascript:document.addnews.submit();\" onclick=\"document.addnews.op.value='save_send'; return control_news();\">Salva e Invia</a> - <!--<a href=\"javascript:;\" onclick=\"openPreview();\">
		 <img  src=\"imgs/page_16x16.gif\" alt=\"Anteprima\" border=0></a>&nbsp;&nbsp;<a href=\"javascript:;\" onclick=\"openPreview();\">Anteprima</a>--></td>
						</tr>
						</form>
						</table>";
			break;

			case "savenews":
			  $onload = "showHide('newsletter2');";
			  $titolo = $_POST['titolo'];
			  $data_sp = $_POST['data_sp'];
			  list($g,$m,$a) = explode("/",$data_sp);
			  $data_sp = "$a-$m-$g";
			  $oggetto = $_POST['oggetto'];
			  $testo = $_POST['testo'];
			  $allegato = $_FILES['allegato'];
			  $idgruppo = $_POST['idgruppo'];
			  $newslet = new newsletter("",$titolo,$data_sp,$oggetto,$testo,$allegato,$idgruppo);
			  $error = $newslet->save();
			  $content = printError($error);
			break;
			
			case "save_send":
			  $onload = "showHide('newsletter2');";
			  $titolo = $_POST['titolo'];
			  $data_sp = $_POST['data_sp'];
			  list($g,$m,$a) = explode("/",$data_sp);
			  $data_sp = "$a-$m-$g";
			  $oggetto = $_POST['oggetto'];
			  $testo = $_POST['testo'];
			  $allegato = $_FILES['allegato'];
			  $idgruppo = $_POST['idgruppo'];
			  $newslet = new newsletter("",$titolo,$data_sp,$oggetto,$testo,$allegato,$idgruppo);
			  $error = $newslet->save();
			  $error = $newslet->send($idgruppo);
			  $content = printError($error);
			break;

			case "modnews":
			  $onload = "showHide('newsletter2');";
			  $idnews = $_GET['id'];
			  $newslet = new newsletter($idnews);
			  list($a,$m,$g) = explode("-",$newslet->data_sp);
			  $data_sp = "$g/$m/$a";
			  if($newslet->allegato != "")
			   $allegato = "&nbsp;&nbsp;&nbsp;<a class=content href=\"newsletter.php?op=removeAtt&idnews=".$newslet->idnews."\">Rimuovi allegato</a>";
			  else
			   $allegato = ""; 
			  $content = "<table class=\"content\" cellspacing=\"0\" cellpadding=\"2\" width=\"700\" bgcolor=#CCCCCC border=\"1px solid #ECECEC\"><form action=\"newsletter.php\" name=\"modnews\" method=\"post\" ENCTYPE=\"multipart/form-data\">
			  			<tr>
						 <td colspan=2 align=center class=tbtit>Modifica newsletter</td>
						</tr>
			  			<tr>
						 <td width=100 align=right>Titolo</td>
						 <td width=600><input type=text size=60 name=titolo value=\"".$newslet->titolo."\"></td>
						</tr>
			  			<tr>
						 <td width=100 align=right>Data</td>
						 <td width=600 valign=middle><input type=text size=10 name=data_sp id=data onBlur=\"check_date(this)\" value=\"".$data_sp."\"><a href=\"javascript:;\" onclick=\"return showCalendar('data', '%d/%m/%Y');\"><img src=\"imgs/date.gif\" border=0 width=20 height=18 alt=\"Calendar\"></a></td>
						</tr>
			  			<tr>
						 <td width=100 align=right>Oggetto</td>
						 <td width=600><input type=text size=60 name=oggetto value=\"".$newslet->oggetto."\"></td>
						</tr>
			  			<tr>
						 <td width=100 align=right valign=top>Testo</td>
						 <td width=600>";
						//start editor
						$oFCKeditor = new FCKeditor('testo');
						$oFCKeditor->Value = "$newslet->testo";
						$content .= $oFCKeditor->Create(); 
 						$content .= "</td></tr>
			  			<tr>
						 <td width=100 align=right>Allegato</td>
						 <td width=600><input type=file size=60 name=allegato>$allegato</td>
						</tr>
			  			<tr>
						 <td width=100 align=right>Gruppo</td>
						 <td width=600>";
						$content .= group::menuGruppi("idgruppo",$newslet->idgruppo,true); 
						$content .= "</td>
						</tr>
						<tr>
						 <td colspan=2 align=center><input type=hidden name=idnews value=".$newslet->idnews.">
						 <input type=hidden name=op value=\"updatenews\">
		 <input type=image src=\"imgs/ico_save.gif\" title=Salva onClick=\"return control_news_edit();\" width=16 height=16>&nbsp;&nbsp;<a class=content href=\"javascript:document.modnews.submit();\">Salva</a> - <!--<a href=\"javascript:;\" onclick=\"openPreviewMod();\">
		 <img  src=\"imgs/page_16x16.gif\" alt=\"Anteprima\" border=0></a>&nbsp;&nbsp;<a href=\"javascript:;\" onclick=\"openPreviewMod();\">Anteprima</a>--> - <a href=\"javascript:document.modnews.submit();\" onclick=\"javascript:document.modnews.op.value='send_news'; \"><img src=\"imgs/ico_mail.gif\" border=0 alt=\"Invia\"></a>&nbsp;&nbsp;<a href=\"javascript:document.modnews.submit();\" onclick=\"javascript:document.modnews.op.value='send_news'; \">Invia</a></td>
						</tr>
						</form>
						</table>";
			  
			break;

			case "updatenews":
			  $onload = "showHide('newsletter2');";
			  $idnews = $_POST['idnews'];
			  $titolo = $_POST['titolo'];
			  $data_sp = $_POST['data_sp'];
			  list($g,$m,$a) = explode("/",$data_sp);
			  $data_sp = "$a-$m-$g";
			  $oggetto = $_POST['oggetto'];
			  $testo = $_POST['testo'];
			  $allegato = $_FILES['allegato'];
			  $idgruppo = $_POST['idgruppo'];
			  $newslet = new newsletter($idnews);
			  $error = $newslet->update($titolo,$data_sp,$oggetto,$testo,$allegato,$idgruppo);
			  $content = printError($error);
			break;

			case "delnews":
			  $onload = "showHide('newsletter2');";
			  $idnews = $_GET['id'];
			  $newslet = new newsletter($idnews);
			  $error = $newslet->delete();
			  $content = printError($error,"newsletter.php?op=allnews");
			break;
			
			case "send_news":
			  $onload = "showHide('newsletter2');";
			  $idnews = $_POST['idnews'];
			  $titolo = $_POST['titolo'];
			  $data_sp = $_POST['data_sp'];
			  list($g,$m,$a) = explode("/",$data_sp);
			  $data_sp = "$a-$m-$g";
			  $oggetto = $_POST['oggetto'];
			  $testo = $_POST['testo'];
			  $allegato = $_FILES['allegato'];
			  $idgruppo = $_POST['idgruppo'];
			  $newslet = new newsletter($idnews);
			  $error = $newslet->update($titolo,$data_sp,$oggetto,$testo,$allegato,$idgruppo);
			  $content = printError($newslet->send($idgruppo));
			break;

			case "allgroup":
			  $onload = "showHide('group');";
			            $min = $_GET['min'];
                        $page = $_GET['page'];
                     if (!isset($min))
                      $min= 0;
                     if (!isset($page))
                      $page = 1;
			  $onload = "showHide('group');";
			  $qry = "SELECT idgruppo,idfather,nome FROM gruppo ORDER BY nome";
			  $newPage = new paging($qry,$min,$num_per_page,"tbtit");
			  $content = $newPage->printRecord("newsletter.php?op=modgroup","newsletter.php?op=delgroup","content","Gruppo padre","Nome");
			  $content .= $newPage->makepage("newsletter.php?op=allgroup",$page);
			break;

			case "addgroup":
			  $onload = "showHide('group');";
			  $content = "<table class=\"content\" cellspacing=\"0\" cellpadding=\"2\" width=\"500\" bgcolor=#CCCCCC border=\"1px solid #ECECEC\"><form action=\"newsletter.php\" name=\"addgroup\" method=\"post\">
			  			<tr>
						 <td colspan=2 align=center class=tbtit>Aggiungi Gruppo</td>
						</tr>
			  		<tr>
					  <td width=100 align=right><b>Gruppo padre:</b></td>
					  <td width=400>";
					  $content .= group::menuGruppi("idfather","all");
					  $content .= "</td>
					  </tr>
					  <tr>
					   <td width=100 align=right><b>Nome gruppo:</b></td>
					   <td width=400><input type=text name=nome size=30></td>
					  </tr>
					  <tr> 
					   <td colspan=2 align=center>
					    <input type=hidden name=op value=savegroup><input type=image src=\"imgs/ico_save.gif\" titlte=Salva onclick=\"return control_group();\">&nbsp;&nbsp;<a href=\"javascript:document.addgroup.submit();\" onclick=\"return control_group();\">Salva</a></td>
					</tr>
				</form>
			</table>";
			break;
			
			case "savegroup":
			  $onload = "showHide('group');";
			  $idfather = $_POST['idfather'];
			  $nome = $_POST['nome'];
			  $group = new group("",$nome,$idfather);
			  $content = printError($group->save());
			break;
			
			case "modgroup":
			  $onload = "showHide('group');";
			  $idgruppo = $_GET['id'];
			  $group = new group($idgruppo);
			  $content = "<table class=\"content\" cellspacing=\"0\" cellpadding=\"2\" width=\"500\" bgcolor=#CCCCCC border=\"1px solid #ECECEC\"><form action=\"newsletter.php\" name=\"modgroup\" method=\"post\">
			  			<tr>
						 <td colspan=2 align=center class=tbtit>Modifica Gruppo</td>
						</tr>
			  		<tr>
					  <td width=100 align=right><b>Gruppo padre:</b></td>
					  <td width=400>";
					  $content .= group::menuGruppi("idfather",$group->idfather);
					  $content .= "</td>
					  </tr>
					  <tr>
					   <td width=100 align=right><b>Nome gruppo:</b></td>
					   <td width=400><input type=text name=nome size=30 value=\"".$group->nome."\"></td>
					  </tr>
					  <tr> 
					   <td colspan=2 align=center>
					   <input type=hidden name=op value=updategroup>
					   <input type=hidden name=idgruppo value=".$group->idgruppo.">
					   <input type=image src=\"imgs/ico_save.gif\" titlte=\"Salva modifiche\">&nbsp;&nbsp;<a href=\"javascript:document.modgroup.submit();\">Salva modifiche</a></td>
					</tr>
				</form>
			</table>";			  
			break;

			case "updategroup":
			  $onload = "showHide('group');";
			  $idgruppo = $_POST['idgruppo'];
			  $idfather = $_POST['idfather'];
			  $nome = $_POST['nome'];
			  $group = new group($idgruppo);
			  $content = printError($group->update($nome,$idfather));
			break;

			case "delgroup":
			  $onload = "showHide('group');";
			  $idgruppo = $_GET['id'];
			  $group = new group($idgruppo);
			  $error = $group->delete();
			  if($error == "0") {
			    Header("Location:newsletter.php?op=allgroup");
				die();
			  }	else
			     $content = printError($error);
			break;

			case "alluser":
			  $onload = "showHide('user');";
			            $min = $_GET['min'];
                        $page = $_GET['page'];
                     if (!isset($min))
                      $min= 0;
                     if (!isset($page))
                      $page = 1;
			  $qry = "SELECT mail,idgruppo FROM contatti ORDER BY creation,mail";
			  $newPage = new paging($qry,$min,$num_per_page,"tbtit");
			  $content = $newPage->printRecord("newsletter.php?op=moduser","newsletter.php?op=deluser","content","Registrazione","Gruppo :: Mail");
			  $content .= $newPage->makepage("newsletter.php?",$page);
			  $content .= "<table width=500 class=content align=center border=1>
			  				 <form action=newsletter.php method=POST>
							  <tr>
							   <td class=tbtit width=200 align=right>Ricerca per gruppo:</td>
							   <td class=tbtit width=300>";
					  			$content .= group::menuGruppi("idgruppo");
					  			$content .= "&nbsp;&nbsp;<input type=submit value=Cerca>
								<input type=hidden name=op value=search_group></td>
							  </tr>
							 </form>
							</table>
							<br>
							<table width=500 class=content align=center border=1>
			  				 <form action=newsletter.php method=POST>
							  <tr>
							   <td class=tbtit width=200 align=right>Esporta per gruppo:</td>
							   <td class=tbtit width=300>";
					  			$content .= group::menuGruppi("idgruppo");
					  			$content .= "&nbsp;&nbsp;<input type=submit value=Esporta>
								<input type=hidden name=op value=get_csv></td>
							  </tr>
							 </form>
							</table>
							<br>
							<table width=500 class=content align=center border=1>
			  				 <form action=newsletter.php method=POST>
							  <tr>
							   <td class=tbtit width=200 align=right>Ricerca per mail:</td>
							   <td class=tbtit width=300><input type=text name=search size=30>&nbsp;&nbsp;<input type=submit value=Cerca>
								<input type=hidden name=op value=search_mail></td>
							  </tr>
							 </form>
							</table>";
			break;
			
			case "get_csv":
			  $idgruppo = $_POST['idgruppo'];
			  contatti::getCsv($idgruppo);
			  die();
			break;

			case "adduser":
			  $onload = "showHide('user');";
			  $content = "<table class=\"content\" cellspacing=\"0\" cellpadding=\"2\" width=\"500\" bgcolor=#CCCCCC border=\"1px solid #ECECEC\"><form action=\"newsletter.php\" name=\"adduser\" method=\"post\">
			  			<tr>
						 <td colspan=2 align=center class=tbtit>Aggiungi Utente</td>
						</tr>
			  		<tr>
					  <td width=100 align=right><b>Gruppo:</b></td>
					  <td width=400>";
					  $content .= group::menuGruppi("idgruppo");
					  $content .= "</td>
					  </tr>
					  <tr>
					   <td width=100 align=right><b>Mail:</b></td>
					   <td width=400><input type=text name=mail size=30></td>
					  </tr>
					  <tr> 
					   <td colspan=2 align=center>
					    <input type=hidden name=op value=saveuser><input type=image src=\"imgs/ico_save.gif\" titlte=Salva onclick=\"return control_user();\">&nbsp;&nbsp;<a href=\"javascript:document.adduser.submit();\" onclick=\"return control_user();\">Salva</a></td>
					</tr>
				</form>
			</table>";
			break;
			
			case "saveuser":
			  $onload = "showHide('user');";
			  $idgruppo = $_POST['idgruppo'];
			  $mail = trim($_POST['mail']);
			  $user = new contatti($mail,$idgruppo);
			  $error = $user->save();
			  $content = printError($error);
			break;

			case "moduser":
			  $onload = "showHide('user');";
			  $mail = $_GET['id'];
			  $user = new contatti($mail);
			  $content = "<table class=\"content\" cellspacing=\"0\" cellpadding=\"2\" width=\"500\" bgcolor=#CCCCCC border=\"1px solid #ECECEC\"><form action=\"newsletter.php\" name=\"moduser\" method=\"post\">
			  			<tr>
						 <td colspan=2 align=center class=tbtit>Modifica Utente</td>
						</tr>
			  		<tr>
					  <td width=100 align=right><b>Gruppo:</b></td>
					  <td width=400>";
					  $content .= group::menuGruppi("idgruppo",$user->idgruppo,true);
					  $content .= "</td>
					  </tr>
					  <tr>
					   <td width=100 align=right><b>Mail:</b></td>
					   <td width=400><input type=text name=mail size=30 value=\"".$user->mail."\" disabled=true></td>
					  </tr>
					  <tr> 
					   <td colspan=2 align=center>
					    <input type=hidden name=op value=updateuser>
						<input type=hidden name=mailupd value=\"".$user->mail."\">
						<input type=image src=\"imgs/ico_save.gif\" titlte=Salva>&nbsp;&nbsp;<a href=\"javascript:document.moduser.submit();\">Salva modifica</a></td>
					</tr>
				</form>
			</table>";
			break;

			case "updateuser":
			  $onload = "showHide('user');";
			  $mail = $_POST['mailupd'];
			  $idgruppo = $_POST['idgruppo'];
			  $user = new contatti($mail);
			  $error = $user->changeGroup($idgruppo);
			  $content = printError($error);
			break;

			case "deluser":
			  $onload = "showHide('user');";
			  $mail = trim($_GET['id']);
			  $user = new contatti($mail);
			  $error = $user->delete();
			  $content = printError($error,"newsletter.php?op=alluser");
			break;
			
			case "search_group":
			  $idgruppo = $_POST['idgruppo'];
			  if($idgruppo == "")
			    $idgruppo = $_GET['idgruppo'];
			  $onload = "showHide('user');";
			            $min = $_GET['min'];
                        $page = $_GET['page'];
                     if (!isset($min))
                      $min= 0;
                     if (!isset($page))
                      $page = 1;
			  $qry = "SELECT mail,idgruppo FROM contatti WHERE idgruppo='$idgruppo' ORDER BY creation,mail";
			  $newPage = new paging($qry,$min,$num_per_page,"tbtit");
			  $content = $newPage->printRecord("newsletter.php?op=moduser","newsletter.php?op=deluser","content","Registrazione","Gruppo :: Mail");
			  $content .= $newPage->makepage("newsletter.php?idgruppo=$idgruppo",$page);
			  $content .= "<table width=500 class=content align=center border=1>
			  				 <form action=newsletter.php method=POST>
							  <tr>
							   <td class=tbtit width=200 align=right>Ricerca per gruppo:</td>
							   <td class=tbtit width=300>";
					  			$content .= group::menuGruppi("idgruppo");
					  			$content .= "&nbsp;&nbsp;<input type=submit value=Cerca>
								<input type=hidden name=op value=search_group></td>
							  </tr>
							 </form>
							</table>";			
			break;
			
			case "search_mail":
			  $search = $_POST['search'];
			  $onload = "showHide('user');";
			  $qry = "SELECT mail,idgruppo FROM contatti WHERE mail LIKE '%$search%' ORDER BY creation,mail";
			  $newPage = new paging($qry,0,1000,"tbtit");
			  $content = $newPage->printRecord("newsletter.php?op=moduser","newsletter.php?op=deluser","content","Registrazione","Gruppo :: Mail");
			  $content .= "<table width=500 class=content align=center border=1>
			  				 <form action=newsletter.php method=POST>
							  <tr>
							   <td class=tbtit width=200 align=right>Ricerca per gruppo:</td>
							   <td class=tbtit width=300>";
					  			$content .= group::menuGruppi("idgruppo");
					  			$content .= "&nbsp;&nbsp;<input type=submit value=Cerca>
								<input type=hidden name=op value=search_group></td>
							  </tr>
							 </form>
							</table>
							<br>
							<table width=500 class=content align=center border=1>
			  				 <form action=newsletter.php method=POST>
							  <tr>
							   <td class=tbtit width=200 align=right>Ricerca per mail:</td>
							   <td class=tbtit width=300><input type=text name=search size=30>&nbsp;&nbsp;<input type=submit value=Cerca>
								<input type=hidden name=op value=search_mail></td>
							  </tr>
							 </form>
							</table>";			
			break;
			
			case "removeAtt":
			  $idnews = $_GET['idnews'];
			  $newslet = new newsletter($idnews);
			  $error = $newslet->removeAllegato();
			  if($error == "0")  {
			    Header("Location:newsletter.php?op=modnews&id=$idnews");
				die();
			  } else
			     $content = printError($error);	
			break;

			default:
			  $content = "&nbsp;";
			break;

		   case "stat":
			 $content = "<table class=\"content\" cellspacing=\"0\" cellpadding=\"2\" width=\"500\" bgcolor=#CCCCCC border=\"1px solid #ECECEC\"><form action=\"newsletter.php\" name=\"stat\" method=\"post\">
							<tr>
							 <td colspan=2 align=center class=tbtit>Crea Report</td>
							</tr>
					<tr>
					 <td width=150 align=right>Dal:</td>
					 <td width=350><input type=text size=10 name=dal id=data1 onBlur=\"check_date(this)\"><a href=\"javascript:;\" onclick=\"return showCalendar('data1', '%d/%m/%Y');\"><img src=\"imgs/date.gif\" border=0 width=20 height=18 alt=\"Calendar\"></a></td>
					</tr>
					<tr>
					 <td width=150 align=right>Al:</td>
					 <td width=350><input type=text size=10 name=al id=data2 onBlur=\"check_date(this)\"><a href=\"javascript:;\" onclick=\"return showCalendar('data2', '%d/%m/%Y');\"><img src=\"imgs/date.gif\" border=0 width=20 height=18 alt=\"Calendar\"></a></td>
					</tr>
					<tr>
					 <td align=right>Ordina per:</td>
					 <td>
					  <select name=orderby>
					   <option value=data>data</option>
					   <option value=gruppo>gruppo</option>
					   <option value=titolo>titolo</option>
					  </select>
					 </td>
					</tr>
							<tr>
							 <td colspan=2 align=center class=tbtit>
							  <input type=hidden name=op value=getReport>
							  <input type=submit value=Vai></td>
							</tr>
				</form>
			  </table>";
		   break;
		   
		   case "getReport":
		     $dal = $_POST['dal'];
			 $al = $_POST['al'];
			 $orderby = $_POST['orderby'];
			 $content = report::listReport($dal,$al,$orderby);
			 if($content == "0")
			   $content = printError("Nessuna newsletter inviata nel periodo selezionato");  
		   break;
		   
		   case "delReport":
		     $idreport = $_GET['id'];
			 $newRep = new report($idreport);
			 $error = $newRep->delete();
			 $content = printError($error,"newsletter.php?op=stat");
		   break;
		}
       break;
	   

       case AUTH_NOT_LOGGED:
        header("Location:index.html");
		die();
       break;
}
?>
<html><!-- InstanceBegin template="/Templates/moduli.dwt.php" codeOutsideHTMLIsLocked="false" -->
<head>
<!-- InstanceBeginEditable name="doctitle" -->
<title>Modulo Newsletter</title>
<!-- InstanceEndEditable -->
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<!-- InstanceBeginEditable name="head" -->
<link rel="stylesheet" type="text/css" media="all" href="skins/aqua/theme.css" title="Aqua">
<script type="text/javascript" src="calendar.js"></script>
<script type="text/javascript" src="calendar-it.js"></script>
<script language="javascript">

var oldLink = null;
function setActiveStyleSheet(link, title) {
  var i, a, main;
  for(i=0; (a = document.getElementsByTagName("link")[i]); i++) {
    if(a.getAttribute("rel").indexOf("style") != -1 && a.getAttribute("title")) {
      a.disabled = true;
      if(a.getAttribute("title") == title) a.disabled = false;
    }
  }
  if (oldLink) oldLink.style.fontWeight = 'normal';
  oldLink = link;
  link.style.fontWeight = 'bold';
  return false;
}

// This function gets called when the end-user clicks on some date.
function selected(cal, date) {
  cal.sel.value = date; // just update the date in the input field.
  if (cal.dateClicked && (cal.sel.id == "sel1" || cal.sel.id == "sel3"))
    cal.callCloseHandler();
}

function closeHandler(cal) {
  cal.hide();                        // hide the calendar
//  cal.destroy();
  _dynarch_popupCalendar = null;
}

function showCalendar(id, format, showsTime, showsOtherMonths) {
  var el = document.getElementById(id);
  if (_dynarch_popupCalendar != null) {
    // we already have some calendar created
    _dynarch_popupCalendar.hide();                 // so we hide it first.
  } else {
    // first-time call, create the calendar.
    var cal = new Calendar(1, null, selected, closeHandler);
    // uncomment the following line to hide the week numbers
    // cal.weekNumbers = false;
    if (typeof showsTime == "string") {
      cal.showsTime = true;
      cal.time24 = (showsTime == "24");
    }
    if (showsOtherMonths) {
      cal.showsOtherMonths = true;
    }
    _dynarch_popupCalendar = cal;                  // remember it in the global var
    cal.setRange(1900, 2070);        // min/max year allowed.
    cal.create();
  }
  _dynarch_popupCalendar.setDateFormat(format);    // set the specified date format
  _dynarch_popupCalendar.parseDate(el.value);      // try to parse the text in field
  _dynarch_popupCalendar.sel = el;                 // inform it what input field we use

  _dynarch_popupCalendar.showAtElement(el.nextSibling, "Br");        // show the calendar

  return false;
}

var MINUTE = 60 * 1000;
var HOUR = 60 * MINUTE;
var DAY = 24 * HOUR;
var WEEK = 7 * DAY;

function isDisabled(date) {
  var today = new Date();
  return (Math.abs(date.getTime() - today.getTime()) / DAY) > 10;
}

function flatSelected(cal, date) {
  var el = document.getElementById("preview");
  el.innerHTML = date;
}

function showFlatCalendar() {
  var parent = document.getElementById("display");

  var cal = new Calendar(0, null, flatSelected);

  cal.weekNumbers = false;

  cal.setDisabledHandler(isDisabled);
  cal.setDateFormat("%A, %B %e");

  cal.create(parent);

  cal.show();
}
</script>
<script language="javascript">

function check_date(field){

var checkstr = "0123456789";

var DateField = field;

var Datevalue = "";

var DateTemp = "";

var seperator = "/";

var day;

var month;

var year;

var step = 0;

var err = 0;

var i;

   err = 0;

   DateValue = DateField.value;

   /* cancello tutti i dati tranne 0-9 */

   for (i = 0; i < DateValue.length; i++) {

              if (checkstr.indexOf(DateValue.substr(i,1)) >= 0) {

                 DateTemp = DateTemp + DateValue.substr(i,1);

              }

   }

   DateValue = DateTemp;

  /* se l'anno � inserito con 2 valori lo cambio sempre con  20xx */

   if (DateValue.length == 6) {

      DateValue = DateValue.substr(0,4) + '20' + DateValue.substr(4,2); }

   if (DateValue.length != 8) {

      err = 19;}

   /* anno sbagliato 0000 */

   year = DateValue.substr(4,4);

   if (year == 0) {

      err = 20;

   }

   /* validazione del mese*/

   month = DateValue.substr(2,2);

   if ((month < 1) || (month > 12)) {

      err = 21;

   }

   /* validazione del giorno */

   day = DateValue.substr(0,2);

   if (day < 1) {

     err = 22;

   }

   /* Validazione anno / febbraio / gg */

   if ((year % 4 == 0)) {

      step = 1;

   }

   if ((month == 2) && (step == 1) && (day > 29)) {

      err = 23;

   }

   if ((month == 2) && (step != 1) && (day > 28)) {

      err = 24;

   }

   /* validazione dei mesi */

   if ((day > 31) && ((month == "01") || (month == "03") || (month == "05") || (month == "07") || (month == "08") || (month == "10") || (month == "12"))) {

      err = 25;

   }

   if ((day > 30) && ((month == "04") || (month == "06") || (month == "09") || (month == "11"))) {

      err = 26;

   }

   /* se � inserito 00 cancello i dati */

   if ((day == 0) && (month == 0) && (year == 00)) {

      err = 0; day = ""; month = ""; year = ""; seperator = "";

   }

   /* Se non ci sono errori inserisco la data (esempio 13.12.2001) */

   if (err == 0) {

      DateField.value = day + seperator + month + seperator + year;

   }

   /* Messaggio di errore, avviso di data corretta se err diverso da 0 */

   else {

      alert("La data non e' corretta.\n\nIl formato consentito e':\n  gg/mm/aaaa\n  - gg/mm/aa\n");

      DateField.select();

              DateField.focus();

   }

}

function MM_showHideLayers() { //v6.0
  var i,p,v,obj,args=MM_showHideLayers.arguments;
  var dis;
  for (i=0; i<(args.length-2); i+=3) if ((obj=MM_findObj(args[i]))!=null) { v=args[i+2];
    if (obj.style) {
	   obj=obj.style; 
	   if(v == 'show')  {
	     v = 'visible';
		 dis = 'block';
	   }
	   if(v == 'hide')  {
	     v = 'hidden';
		 dis = 'none';	 
	   }
	 }  
    obj.visibility = v; 
	obj.display = dis;
	}
}

function showHide(id)  {
	var obj;
	obj=MM_findObj(id);
	if(obj.style.visibility=='visible')
	 MM_showHideLayers(id,'','hide');
	else  {
	 MM_showHideLayers(id,'','show');
	}
}

function openPreview()  {
   document.addnews.action = "anteprima.php";
   newin = window.open("","anteprima","scrollbars=yes,resizable=no,width=800,height=600,status=no,location=no,toolbar=no");
   document.addnews.target = "anteprima";
   document.addnews.submit();  
}

function openPreviewMod()  {
   document.modnews.action = "anteprima.php";
   newin = window.open("","anteprima","scrollbars=yes,resizable=no,width=800,height=600,status=no,location=no,toolbar=no");
   document.modnews.target = "anteprima";
   document.modnews.submit();  
}

function control_news()  {
  if(document.addnews.titolo.value=="")  {
    alert("Devi inserire il titolo!");
	return false;
  }
  if(document.addnews.oggetto.value=="")  {
    alert("Devi inserire l'oggetto!");
	return false;
  }
  if(document.addnews.idgruppo.options[document.addnews.idgruppo.selectedIndex].value=="0")  {
    alert("Devi selezionare un gruppo!");
	return false;
  }
  return true;
}

function control_news_edit()  {
  if(document.modnews.titolo.value=="")  {
    alert("Devi inserire il titolo!");
	return false;
  }
  if(document.modnews.oggetto.value=="")  {
    alert("Devi inserire l'oggetto!");
	return false;
  }
  if(document.modnews.idgruppo.options[document.modnews.idgruppo.selectedIndex].value=="0")  {
    alert("Devi selezionare un gruppo!");
	return false;
  }
  return true;
}

function control_group()  {
  if(document.addgroup.nome.value=="")  {
    alert("Devi inserire il nome del gruppo!");
	return false;
  }
  return true;
}

function control_user()  {
  if(document.adduser.idgruppo.options[document.adduser.idgruppo.selectedIndex].value=="0")  {
    alert("Devi selezionare il gruppo!");
	return false;
  }
  if(document.adduser.mail.value=="")  {
    alert("Devi inserire la mail!");
	return false;
  }
  return true;
}

//-->
</script>
<!-- InstanceEndEditable -->
<link href="style/style.css" rel="stylesheet" type="text/css">
<script type="text/JavaScript">
<!--
<?=$javascript?>

function MM_showHideLayers() { //v6.0
  var i,p,v,obj,args=MM_showHideLayers.arguments;
  var dis;
  for (i=0; i<(args.length-2); i+=3) if ((obj=MM_findObj(args[i]))!=null) { v=args[i+2];
    if (obj.style) {
	   obj=obj.style; 
	   if(v == 'show')  {
	     v = 'visible';
		 dis = 'block';
	   }
	   if(v == 'hide')  {
	     v = 'hidden';
		 dis = 'none';	 
	   }
	 }  
    obj.visibility = v; 
	obj.display = dis;
	}
}

function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

function MM_swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}

function MM_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}
//-->
</script>
</head>
<body bgcolor="#FFFFFF" leftmargin="0" topmargin="0" marginwidth="0" marginheight="0" onLoad="MM_preloadImages('imgs/icons/pag_web-b.gif','imgs/icons/info_utente-b.gif','imgs/icons/prodotti-b.gif','imgs/icons/newsletter-b.gif','imgs/icons/news-b.gif','imgs/icons/gest_banner-b.gif','imgs/icons/utenti-b.gif','imgs/icons/configurazione-b.gif','imgs/icons/kw-b.gif'); <?=$onload?>">
<!-- ImageReady Slices (gestione.psd - Slices: 02, 03, 04, 06, 07, 08, 09, 10, 11, 12) -->
<!-- End ImageReady Slices -->
<table width="100%" height="100%" border="0" cellpadding="0" cellspacing="0">
  <tr>
    <td width="40" height="177" background="imgs/bkgSx.gif">&nbsp;</td>
    <td><table width="100%" border="0" cellspacing="0" cellpadding="0">
      <tr>
        <td width="552" background="imgs/gestione_02.gif"><table width="100%" border="0" cellspacing="0" cellpadding="0">
          <tr>
            <td width="360"><img src="imgs/spacer20.gif" width="360" height="73"></td>
            <td><table width="100%" height="73" border="0" cellpadding="0" cellspacing="0">
              <tr>
                <td height="5"><img src="imgs/spacer20.gif" width="45" height="5"></td>
              </tr>
              <tr>
                <td align="left" valign="middle" class="tit01"><!-- InstanceBeginEditable name="user" -->
                  <?=$username?>
                <!-- InstanceEndEditable --></td>
              </tr>
            </table></td>
          </tr>
        </table></td>
        <td background="imgs/gestione_03.gif">&nbsp;</td>
        <td width="221"><table width="100%" border="0" cellspacing="0" cellpadding="0">
          <tr>
            <td><img src="imgs/gestione_04.gif" width="221" height="17" alt=""></td>
          </tr>
          <tr>
            <td><table width="100%" border="0" cellspacing="0" cellpadding="0">
              <tr>
                <td width="57"><a href="gestione.php?logout=1"><img src="imgs/gestione_06.gif" alt="Logout" width="57" height="51" border="0"></a></td>
                <td><table width="100%" border="0" cellspacing="0" cellpadding="0">
                  <tr>
                    <td><a href="mailto:info@media-tek.it"><img src="imgs/gestione_07.gif" alt="" width="164" height="25" border="0"></a></td>
                  </tr>
                  <tr>
                    <td><a href="#"><img src="imgs/gestione_08.gif" alt="" width="164" height="26" border="0"></a></td>
                  </tr>
                </table></td>
              </tr>
            </table></td>
          </tr>
          <tr>
            <td><img src="imgs/gestione_09.gif" width="221" height="5" alt=""></td>
          </tr>
        </table></td>
      </tr>
    </table>
      <table width="100%" height="104" border="0" cellpadding="0" cellspacing="0">
        <tr>
          <td width="678" background="imgs/gestione_10.gif"><table width="100%" border="0" cellspacing="0" cellpadding="0">
            <tr>
              <td width="203"><table width="203" height="104" border="0" cellpadding="0" cellspacing="0">
                <tr>
                  <td><img src="imgs/spacer20.gif" width="203" height="65"></td>
                </tr>
                <tr>
                  <td><table width="100%" border="0" cellspacing="0" cellpadding="0">
                    <tr>
                      <td width="45"><img src="imgs/spacer20.gif" width="45" height="39"></td>
                      <td><table width="100%" height="39" border="0" cellpadding="5" cellspacing="0">
                        <tr>
                          <td align="left" valign="middle" class="module"><!-- InstanceBeginEditable name="module" -->
                            <?=$module?>
                          <!-- InstanceEndEditable --></td>
                        </tr>
                      </table></td>
                    </tr>
                  </table></td>
                </tr>
              </table></td>
              <td><table width="475" border="0" cellspacing="0" cellpadding="0">
                <tr>
                  <td width="50"><img src="imgs/spacer20.gif" width="50" height="104" alt=""></td>
                  <td align="left" valign="bottom">
				  <? if($level!="VBL")  { ?>
				  <table border="0" cellspacing="0" cellpadding="2"><tr><?
	if($level=="admin")  {
?><td width="58"><a href="gestione.php?module=content" onMouseOut="MM_swapImgRestore()" onMouseOver="MM_swapImage('pages','','imgs/icons/pag_web-b.gif',1)"><img src="imgs/icons/pag_web.gif" alt="Pagine web" name="pages" width="58" height="69" border="0"></a></td><td width="58"><a href="gestione.php?module=newsletter" onMouseOut="MM_swapImgRestore()" onMouseOver="MM_swapImage('Newsletter','','imgs/icons/newsletter-b.gif',1)"><img src="imgs/icons/newsletter.gif" alt="Newsletter" name="Newsletter" width="58" height="69" border="0"></a></td><?
	}
	if($level=="admin")  {
?><td width="58"><a href="gestione.php?module=banner" onMouseOut="MM_swapImgRestore()" onMouseOver="MM_swapImage('banner','','imgs/icons/gest_banner-b.gif',1)"><img src="imgs/icons/gest_banner.gif" alt="Gestione Banner" name="banner" width="58" height="69" border="0"></a></td><?
	}
?><td width="58"><a href="gestione.php?module=prodotti"><img src="imgs/icons/prodotti.gif" alt="Pagine Web" name="pagine" width="58" height="69" border="0" id="pagine" onMouseOver="MM_swapImage('pagine','','imgs/icons/prodotti-b.gif',1)" onMouseOut="MM_swapImgRestore()"></a></td>
</tr></table>
<? } ?>
</td>
				</tr>
              </table></td>
            </tr>
          </table></td>
          <td valign="bottom" background="imgs/gestione_11.gif"><?
	if($level=="admin")  {
?><table cellpadding="2" cellspacing="0">
		    <tr>
			  <td width="58"><a href="gestione.php?module=webuser" onMouseOut="MM_swapImgRestore()" onMouseOver="MM_swapImage('webutente','','imgs/icons/utenti-b.gif',1)"><img src="imgs/icons/utenti.gif" name="webutente" width="58" height="69" border="0"></a></td>
			  <td width="58"><a href="gestione.php?module=user" onMouseOut="MM_swapImgRestore()" onMouseOver="MM_swapImage('Utente','','imgs/icons/info_utente-b.gif',1)"><img src="imgs/icons/info_utente.gif" name="Utente" width="58" height="69" border="0"></a></td>
			  <td width="20" valign="middle">
			   </td>			  
			</tr>
		   </table><?
		   } else {
?>&nbsp;<?
}
		   ?></td>
          <td width="95"><img src="imgs/gestione_12.gif" width="95" height="104" alt=""></td>
        </tr>
      </table>
    </td>
    <td width="40" background="imgs/bkgDx.gif">&nbsp;</td>
  </tr>
  <tr>
    <td background="imgs/bkgSx.gif">&nbsp;</td>
    <td valign="top" bgcolor="#F6F6F6"><table width="100%" border="0" cellpadding="0" cellspacing="0">
      <tr>
        <td width="203" align="left" valign="top" bgcolor="#DFDFDF" class="menusx"><!-- InstanceBeginEditable name="menuSx" -->
          <?=$menusx?>
        <!-- InstanceEndEditable --></td>
        <td align="left" valign="top" bgcolor="#EFEFEF" class="content"><!-- InstanceBeginEditable name="content" -->
          <?=$content?>
        <!-- InstanceEndEditable --></td>
      </tr>
    </table>
      <table width="100%" height="50" border="0" cellpadding="5" cellspacing="0">
        <tr>
          <td background="imgs/gestione_03.gif" bgcolor="#F6F6F6" class="copy"><p>2006&copy; Gestione moduli dinamici - release 1.2 - Powered by Media-tek </p></td>
        </tr>
      </table></td>
    <td background="imgs/bkgDx.gif">&nbsp;</td>
  </tr>
</table>
</body>
<!-- InstanceEnd --></html>